﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using SmartProduction.Domain.客户管理;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Read.API.Application.Commands.SalesOrderCommand;
using SmartProduction.Read.API.Dtos;

namespace SmartProduction.Read.API.Application.CommandHandlers.SalesOrderHandler
{
    /// <summary>
    /// 销售订单查询命令处理
    /// </summary>
    public class SalesOrderQueryCommandHandler : IRequestHandler<SalesOrderQueryCommand, APIResult<APIPaing<SalesOrder>>>
    {
        /// <summary>
        /// 销售订单仓储
        /// </summary>
        private readonly IBaseRepository<SalesOrder> salesRepository;

        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="salesRepository">销售订单仓储</param>
        /// <param name="mapper">映射</param>
        public SalesOrderQueryCommandHandler(IBaseRepository<SalesOrder> salesRepository,IMapper mapper)
        {
            this.salesRepository = salesRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public Task<APIResult<APIPaing<SalesOrder>>> Handle(SalesOrderQueryCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<APIPaing<SalesOrder>> result = new APIResult<APIPaing<SalesOrder>>();
                result.Code = APIEnums.查询成功;
                result.Msg = APIEnums.查询成功.ToString();
                var list = salesRepository.GetAll().Result;
                if (!string.IsNullOrEmpty(request.SalesOrderNumer))
                {
                    list = list.Where(x => x.SalesOrderNumer == request.SalesOrderNumer);
                }
                if (!string.IsNullOrEmpty(request.ClientInfoNumer))
                {
                    list = list.Where(x => x.ClientInfoNumer == request.ClientInfoNumer);
                }
                if (!string.IsNullOrEmpty(request.ClientInfoNo))
                {
                    list = list.Where(x => x.ClientInfoNo == request.ClientInfoNo);
                }
                if (!string.IsNullOrEmpty(request.AltEnter))
                {
                    list = list.Where(x => x.AltEnter == request.AltEnter);
                }
                if (!string.IsNullOrEmpty(request.SalesOrderType))
                {
                    list = list.Where(x => x.SalesOrderType == request.SalesOrderType);
                }
                if (request.OrderStatus != null)
                {
                    list = list.Where(x => x.OrderStatus == request.OrderStatus);
                }

                var count = list.Count();
                var page = (int)Math.Ceiling(count * 1.0 / request.PageSize);
                var models = list.OrderByDescending(x => x.SalesOrderId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
                APIPaing<SalesOrder> paging = new APIPaing<SalesOrder>();
                paging.PageCount = page;
                paging.TotalCount = count;
                paging.Items = mapper.Map<List<SalesOrder>>(models);
                result.data = paging;
                return Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
